//
//  ComposeViewController.swift
//  zyqWeibo
//
//  Created by 张亚琼 on 15/7/9.
//  Copyright © 2015年 张亚琼. All rights reserved.
//

import UIKit
import FFAutoLayout

class ComposeViewController: UIViewController {

    func close() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    func sendWeibo() {
        
    }
    override func loadView() {
        view = UIView()
        view.backgroundColor = UIColor.orangeColor()
        // 准备导航栏
        prepareNavigationBar()
        prepareTextView()
    
        
    }
    
    func inputEmotion() {
        print(__FUNCTION__)
    }
    func selectPic(){
        
        print(__FUNCTION__)
    }
    
    // 准备toolBar
    func prepareToolBar() -> UIToolbar {
        let toolBar = UIToolbar(frame: CGRectMake(0, 0, 0, 44))
        toolBar.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
        
        let itemSetting = [["imageName":"compose_toolbar_picture","action":"selectPic"],["imageName":"compose_mentionbutton_background","action":"selectPic"],["imageName":"compose_trendbutton_background","action":"selectPic"],["imageName":"compose_emoticonbutton_background","action":"inputEmotion"],["imageName":"compose_addbutton_background","action":"selectPic"]]
        
        // 所有按钮的数组
        var items = [UIBarButtonItem]()
        // 设置按钮
        for settings in itemSetting {
            items.append(UIBarButtonItem(imageName: settings["imageName"]!, hilightedImage: nil, target: self, actionName: settings["action"]))
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        }
        // 删除最后一个弹簧
        items.removeLast()
        toolBar.items = items
        
        return toolBar
        
    }
    // 准备文本输入框
    func prepareTextView() {
        let textView = UITextView()
        view.addSubview(textView)
        textView.ff_Fill(view)
        
        textView.inputAccessoryView = prepareToolBar()
        textView.text = "分享新鲜事。。。分享新鲜事。。。分享新鲜事。。。分享新鲜事。。。分享新鲜事。。。分享新鲜事。。。"
        textView.font = UIFont.systemFontOfSize(18)
        // 设置文本框垂直滚动
        textView.alwaysBounceVertical = true
        // 设置拖动文本框就关闭键盘
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        
        placeHodlerLabel.text = "分享新鲜事。。。"
        placeHodlerLabel.sizeToFit()
        textView.addSubview(placeHodlerLabel)
        placeHodlerLabel.ff_AlignInner(ff_AlignType.TopLeft, referView: textView, size: nil,offset:CGPoint(x: 12, y: 8))
    }
    // 准备导航栏
    func prepareNavigationBar(){
        // 左侧按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        // 右侧按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendWeibo")
        // 导航栏
        let titleV = UIView(frame: CGRectMake(0, 0, 100, 32))
        let label1 = UILabel(color: UIColor.darkGrayColor(), foneSize: 14)
        label1.text = "发微博"
        label1.sizeToFit()
        let label2 = UILabel(color: UIColor.lightGrayColor(), foneSize: 13)
        label2.text = sharedUserAccount?.name ?? ""
        label2.sizeToFit()
        titleV.addSubview(label1)
        titleV.addSubview(label2)
        // 设置布局
        label1.ff_AlignInner(ff_AlignType.TopCenter, referView: titleV, size: nil)
        label2.ff_AlignInner(ff_AlignType.BottomCenter, referView: titleV, size: nil)
       
        
        navigationItem.titleView = titleV
    }
    
    lazy var placeHodlerLabel:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 18))

}
